package com.gonsin.license;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class JsonUtils {

    private static Logger logger = Logger.getLogger(JsonUtils.class);

    /**
     * 对象转json
     * @param obj
     * @return
     */
    public static String toJson(Object obj){
        Gson gson = new GsonBuilder()
                .enableComplexMapKeySerialization()
//            .serializeNulls()
                .disableHtmlEscaping().create();
        return gson.toJson(obj);
    }

    /**
     * json转map
     * @param json
     * @return
     */
    public static Map<String,Object> fromJson(String json) {
        if(json == null){
            return new HashMap<String,Object>();
        }
        Gson gson = new GsonBuilder()
                .enableComplexMapKeySerialization()
                .disableHtmlEscaping().create();
        return gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
    }


}
